package com.app.service.impl;

import com.app.mapper.DepartmentMapper;
import com.app.mapper.PersonMapper;
import com.app.model.DepartmentModel;
import com.app.service.DepartmentService;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    PersonMapper personMapper;

    @Override
    public DepartmentModel getDepByDepId(int id) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        DepartmentModel model = departmentMapper.selectOne(queryWrapper);
        if (model == null) {
            return null;
        } else {
            return model;
        }
    }

    @Override
    public List<DepartmentModel> getDepByDepIdList(int id) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "short_name", "pid", "level", "is_cards", "sort", "status").eq("id", id);
        List<DepartmentModel> list = departmentMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public DepartmentModel getDepLevelByPId(int pid) {//查等级
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "pid", "level", "is_cards", "sort", "status").eq("id", pid);
        DepartmentModel model = departmentMapper.selectOne(queryWrapper);
        if (model == null) {
            return null;
        } else {
            return model;
        }
    }

    @Override
    public List<DepartmentModel> getListByPid(int pid) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "short_name", "pid", "level", "is_cards", "sort", "status").eq("pid", pid);
//         if(StringTool.isNotEmpty(keyWord)){
//             queryWrapper.like("name",keyWord);
//         }
        List<DepartmentModel> list = departmentMapper.selectList(queryWrapper);
        return list;
    }

    //    @Override
//    public List<DepartmentModel> getListByPid(int pid,String keyWord) {
//        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("id","name","short_name","pid","level","is_cards","sort","status").eq("pid", pid);
//        if(StringTool.isNotEmpty(keyWord)){
//            queryWrapper.like("name",keyWord);
//        }
//        List<DepartmentModel> list =  departmentMapper.selectList(queryWrapper);
//        return list;
//    }
    @Override
    public Page<DepartmentModel> getPageDepList(int page, int limit, String name, String shortName) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if(name != null){
            queryWrapper.like("name", name);
        }
        if(shortName != null){
            queryWrapper.like("short_name", shortName);
        }
        queryWrapper.eq("status",1).orderByDesc("id");
        Page<DepartmentModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<DepartmentModel> depList = departmentMapper.selectPage(pages, queryWrapper);
        return depList;
    }

    @Override
    public Integer getNumByPid(Integer pid) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "pid", "level").eq("pid", pid);
        Integer num = departmentMapper.selectCount(queryWrapper);
        return num;
    }

    @Override
    public Integer DepCount() {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        Integer depCount = departmentMapper.selectCount(queryWrapper);
        return depCount;
    }

    @Override
    public Integer DepartmentCount(List<Integer> idsList) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if (idsList != null&&idsList.size()>0) {
            queryWrapper.in("id", idsList);
        }
        Integer depCount = departmentMapper.selectCount(queryWrapper);
        return depCount;
    }

    @Override
    public List<DepartmentModel> getListByIds(String[] ids) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name").in("id", ids);
        List<DepartmentModel> list = departmentMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Integer select(Integer adminUserId) {
        return departmentMapper.select(adminUserId);
    }

    @Override
    public List<DepartmentModel> getDepTree(Integer pid, Integer level, String name) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("pid", pid).eq("level", level);
        if (StringTool.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByAsc("id");
        List<DepartmentModel> depTree = departmentMapper.selectList(queryWrapper);
        return depTree;
    }

    @Override
    public List<DepartmentModel> selectDepTree(String keyWord,List<Integer> idsList) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        if(idsList!=null&&idsList.size()>0){
                queryWrapper.in("id", idsList);
            if (StringTool.isNotEmpty(keyWord)) {
                queryWrapper.like("name", keyWord);
            }
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.orderByAsc("sort");
        List<DepartmentModel> depTree = departmentMapper.selectList(queryWrapper);
        return depTree;
    }

    @Override
    public List<DepartmentModel> getAll() {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name").orderByAsc("sort");
        List<DepartmentModel> depTree = departmentMapper.selectList(queryWrapper);
        return depTree;
    }

    @Override
    public void update(DepartmentModel model) {
        departmentMapper.updateById(model);
    }

    @Override
    public void insert(DepartmentModel model) {
        departmentMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        departmentMapper.deleteById(id);
    }

    @Override
    public List<Integer> getDepId(Integer departmentId) {
        return null;
    }


}
